package com.wy.tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wy.tingshu.service.AlbumService;
import com.wy.tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 关联音频（专辑+专辑相关的音频列表；所有候选音频）
 **/
//不考虑错误处理，抛异常出去
@WebServlet("/studio/album/bind.json")
public class BindJsonServlet extends HttpServlet {
    private final AlbumService albumService=new AlbumService();
    private final ObjectMapper objectMapper=new ObjectMapper();//用于序列化
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if(session==null){
            throw new RuntimeException("用户未登录");
        }
        UserVO currentUser = (UserVO) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new RuntimeException("用户未登录");
        }
        req.setCharacterEncoding("utf-8");
        int aid=Integer.parseInt(req.getParameter("aid"));

        Object view=albumService.getBindInfo(currentUser,aid);//通过uid(用户),aid(音频)获取已有专辑
        String json = objectMapper.writeValueAsString(view);//view对象转换为json形式的字符串
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);

    }
}










